"""
本文件仅供方便添加新的服务，无具体作用
"""

import queue
import logging
import threading
import time
from datetime import datetime

class Demo:
    def __init__(self):
        self.run = False
        self.__log_init()


    def __log_init(self):
        self.logger = logging.getLogger("This")


    def open(self):
        self.logger.trace('open.enter')
        
        # TODO:任务初始化
        
        self.logger.trace('open.done')
        return True


    def close(self):
        self.logger.trace('close.enter')
        
        # TODO:任务清理

        self.logger.trace('close.done')


    def thread(self):
        self.logger.trace('thread.enter')

        while self.run:
            time.sleep(0.5)
            self.logger.warning("This is the demo, please close it!")

        self.logger.trace('thread.done')


    def start(self):
        self.logger.trace('start.enter')

        self.run = True
        self.handle = threading.Thread(target=self.thread)
        self.handle.start()

        self.logger.trace('start.done')
        return True


    def stop(self):
        self.logger.trace('stop.enter')
        self.run = False
        self.handle.join()
        self.logger.trace('stop.done')


    def input_queue_set(self, input_queue_list):
        # 设置输入队列，接受的是一个队列列表
        self.logger.trace('input_queue_set.enter')

        if self.run:
            self.logger.error("this is opened, please close the this first!")
            return False
        
        if not isinstance(input_queue_list, list):
            input_queue_list = [input_queue_list]

        for input_queue in input_queue_list:
            if not isinstance(input_queue, queue.Queue):
                self.logger.error("one queue is not a queue!")
                return False

        # 针对项目需求，设置服务输入的队列
        # self.inqueue1 = input_queue_list[0] 

        self.logger.trace('input_queue_set.done')
        return True


    def output_queue_set(self, output_queue_list):
        # 设置输出队列，接受的是一个队列列表
        self.logger.trace('output_queue_set.enter')

        if self.run:
            self.logger.error("this is opened, please close the this first!")
            return False
        
        if not isinstance(output_queue_list, list):
            output_queue_list = [output_queue_list]

        for output_queue in output_queue_list:
            if not isinstance(output_queue, queue.Queue):
                self.logger.error("one queue is not a queue!")
                return False

        # 针对项目需求，设置服务输出的队列
        # self.outqueue1 = output_queue_list[0] 

        self.logger.trace('output_queue_set.done')
        return True